<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowe');

class Main extends CI_Controller {

    function __construct() { //inisialisasi library dan helper yang perlu di load
        parent::__construct();
        $this->load->model('simades');
    }

    function index() {
        $data['title'] = 'Welcome';
        $data['isi'] = $this->simades->get_sys();
        $this->load->view('main', $data);
    }

    function login() {
        $this->form_validation->set_rules('name', 'Username', 'required');
        $this->form_validation->set_rules('pass', 'Password', 'required');
        if ($this->form_validation->run() == FALSE) {
            redirect();
        } else {
            $log = array(
                'username' => $this->input->post('name'),
                'password' => $this->encrypt->sha1($this->input->post('pass'))
            );
            $login = $this->simades->login($log); //kirim ke database
            if ($login->privilege_id == 1) {
                $ses = array(
                    'username' => $login->username,
                    'nama' => $login->nama,
                    'login' => TRUE,
                    'privilege' => 1
                );
                $this->session->set_userdata($ses);
                redirect('admin');
            } elseif ($login->privilege_id == 2) {
                $ses = array(
                    'username' => $login->username,
                    'nama' => $login->nama,
                    'login' => TRUE,
                    'privilege' => 2
                );
                $this->session->set_userdata($ses);
                redirect('petugas');
            } else {
                redirect();
            }
        }
    }

    function about() {
        
    }

    function contact() {
        $data['title'] = 'Contact';
        $data['username'] = $this->session->userdata('username');
        if ($data['isi'] = $this->simades->get_sys()) {
            $data['isi'] = $this->simades->get_sys();
        } else {
            $data['isi'] = array();
        }
        $this->load->view('template/head', $data);
        $this->load->view('template/menu');
        $this->load->view('template/alert', $data);
        $this->load->view('petugas/home', $data);
        $this->load->view('template/foot');
    }

    function logout() {
        $this->session->sess_destroy();
        redirect();
    }

    function notfound() {
        $this->load->view('notfound');
    }

}

?>